<template>
  <div class="home">
    <div class="content">
      <div class="header">
        <el-page-header content="编辑页面" @back="goBack"></el-page-header>
      </div>
      <div class="selectMoudle">
        <div>
          <span class="text">模块</span>
          <el-radio-group v-model="editorData.radio1" fill="#1d2f70">
            <el-radio-button v-for="(item, index) in radio" :label="item.label" :key="index"></el-radio-button>
          </el-radio-group>
        </div>
        <div>
          <el-button class="sendBtn" @click="send">
            发布
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </div>
      </div>
      <div class="inputBox">
        <input type="text" placeholder="请写下你的问题" v-model="editorData.title" />
      </div>
      <div class="editor">
        <textEditor
          @EditorChange="editorChange"
          @uploadImageChange="uploadImageChange"
          :oldcontent="editorData.html"
          :oldremoveIds="editorData.deleImgId"
        ></textEditor>
      </div>
    </div>
  </div>
</template>

<script>
// import axios from "axios";
import textEditor from "@/components/quillEditor";
import { get_draft, put_draft, delete_draft } from "@/api/draft";
import { post_problem } from "@/api/problem";
import { post_delectImg } from "@/api/deleteTheInterface";
import { textReplace } from "@/common/textReplace";
export default {
  name: "Editor",
  data() {
    return {
      editorData: {
        radio1: "互帮互助",
        title: "",
        titleImageUrl: "",
        html: "",
        deleImgId: [],
        textReplace: "",
        deletTitleImageUrl: []
      },
      timer: null,
      radio: [
        {
          name: "互帮互助",
          label: "互帮互助"
        }
      ]
    };
  },
  components: {
    textEditor
  },
  async created() {
    console.log(this.$route.query._id);
    if (!this.$route.query._id) {
      this.$router.push({
        path: "/home"
      });
      return;
    }
    let editData = await get_draft({ query: { _id: this.$route.query._id } });
    console.log("editData", editData.data[0]);
    this.editorData = editData.data[0];
    this.$message({
      showClose: true,
      message: "已自动保存成功至草稿箱",
      type: "success"
    });
    clearInterval(this.timer);
    this.timer = null;
    this.setTimer();
  },
  destroyed: function() {
    // 每次离开当前界面时，清除定时器
    clearInterval(this.timer);
    this.timer = null;
  },
  methods: {
    setTimer() {
      let time = 1000 * 30;
      if (this.timer == null) {
        this.timer = setInterval(async () => {
          console.log("setTimer保存");
          this.editorData.editTime = new Date();
          await put_draft({
            query: { _id: this.editorData._id },
            update: {
              $set: this.editorData
            }
          });
          this.$message({
            showClose: true,
            message: "自动保存成功",
            type: "success"
          });
        }, time);
      }
    },
    async uploadImageChange() {
      console.log("uploadImageChange保存");
      await put_draft({
        query: { _id: this.editorData._id },
        update: {
          $set: this.editorData
        }
      });
      this.$message({
        showClose: true,
        message: "自动保存成功",
        type: "success"
      });
    },
    goBack() {
      this.$router.go(-1);
    },
    editorChange(data) {
      this.editorData.html = data.html;
      if (this.editorData.deleImgId.length != data.removeIds.length) {
        this.uploadImageChange();
      }
      this.editorData.deleImgId = data.removeIds;
      // this.editorData.textReplace = textReplace(data.html);
      // console.log("改变后", this.editorData);
    },
    //发布文章
    async send() {
      if (this.editorData.title.replace(/\s+/g, "").length < 4) {
        return this.$alert("请完善问题描述,至少输入 4 个字", "提示", {
          confirmButtonText: "确定",
          callback: () => {}
        });
      }
      // if (this.editorData.html == "") {
      //   return this.$alert("请完善文章内容", "提示", {
      //     confirmButtonText: "确定"
      //   });
      // }
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let deleArr = this.editorData.deleImgId;
      console.log(deleArr);
      this.editorData.textReplace = textReplace(this.editorData.html);
      await post_problem({
        problemname: this.editorData.title, //问题标题
        content: this.editorData.html, //富文本内容
        userid: this.$store.getters.userInfo._id, //用户id
        problemtype: this.editorData.radio1, //文章类型
        textReplace: this.editorData.textReplace
      });

      let dele = await delete_draft({
        query: {
          _id: this.editorData._id
        }
      });
      console.log("dele", dele);

      if (deleArr.length != 0) {
        await post_delectImg({
          deleImgArr: deleArr
        });
      }
      setTimeout(() => {
        loading.close();
        this.$message({
          showClose: true,
          message: "发布成功",
          type: "success"
        });
        this.$alert("问题已发布，请耐心等待审核", "提示", {
          confirmButtonText: "确定",
          callback: () => {
            this.$router.replace({
              path: "/help"
            });
          }
        });
        // this.$router.go(-1);

        this.$finish_taskdata(2, this.$store.getters.userInfo._id);
      }, 1000);
    }
  }
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  background: #fff;
}
.content {
  width: 100%;
  .header {
    margin: 0 auto;
    width: 1200px;
    margin-top: 20px;
  }
  .selectMoudle {
    margin: 0 auto;
    width: 1200px;
    margin-top: 40px;
    display: flex;
    justify-content: space-between;
    .text {
      font-size: 20px;
      font-weight: 700;
      color: #757575;
      margin-right: 20px;
    }
    .sendBtn {
      background: #1d2f70;
      border: #1d2f70;
      color: #fff;
      &:hover {
        background: rgb(53, 89, 180);
      }
    }
  }
  .inputBox {
    margin: 0 auto;
    width: 1200px;
    margin-top: 30px;
    input {
      outline-style: none;
      outline-style: none;
      border: 0px;
      border-bottom: 2px solid #ccc;
      width: 1200px;
      padding: 13px 14px;
      font-size: 28px;
      font-weight: 700;
      font-family: "Microsoft soft";
      box-sizing: border-box;
    }
  }
  // margin-top: -19px;
  padding: 20px;
  box-sizing: border-box;
  .editor {
    min-height: 500px;
    margin-top: 40px;
  }
}
</style>
